<?php

namespace app\admin\model;

use app\common\model\CateModel;
use app\common\model\SourceModel;
use think\helper\Arr;
use think\Model;


class TeamSourceModel extends Model
{


    // 表名
    protected $name = 'team_source';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'create_time';
    protected $updateTime = false;
    protected $deleteTime = false;

    // 追加属性
    protected $append = [

    ];

    public function cate()
    {
        return $this->belongsTo(CateModel::class, 'cid', 'id', [], 'LEFT');
    }

    public function admin()
    {
        return $this->belongsTo(AdminUser::class, 'aid', 'id', [], 'LEFT');
    }


    public function getTeamSource($params, $aid)
    {
        $map = [];
        if (!isset($params['status']) || !$params['status']) {
            return ['code' => 201, 'msg' => '缺少参数'];
        }

        if (isset($params['title']) && $params['title']) {
            $map[] = ['yzn_team_source.title', 'like', "%" . $params['title'] . "%"];
        }
        $map[] = ['yzn_team_source.status', '=', $params['status']];
        $map[] = ['yzn_team_source.aid', '=', $aid];
        $map[] = ['yzn_team_source.is_lock', '=', 0];
        if ($params['cid']) {
            $map[] = ['yzn_team_source.cid', '=', $params['cid']];
        }
        $list = self::where($map)
            ->withJoin(['cate'])
            ->order("sort desc")
            ->paginate($params['limit'], false, ['page' => $params['page']]);
        foreach ($list as $row) {
            $row->getRelation('cate')->visible(['cate_name']);
            $row->visible(['id', 'title', 'annex', 'kind', 'create_time','hashtags','cid','sort']);
        }
        return ['code' => 200, 'msg' => '获取信息成功', 'data' => $list];
    }


    public function addTeamSource($params, $aid)
    {
        if (!$params['cid'] || !$params['title'] || !$params['annex'] || !$params['kind'] || !$params['hashtags']) {
            return ['code' => 201, 'msg' => '缺少参数'];
        }
        $params['aid'] = $aid;
        $model = new self();
        if(!$model->allowField(true)->save($params)){
            return ['code' => 201, 'msg' => '保存失败'];
        }
        return ['code' => 200, 'msg' => '添加成功'];
    }


    public function updateTeamSource($params, $aid)
    {
        if (!$params['cid'] || !$params['title'] || !$params['annex'] || !$params['kind'] || !$params['hashtags']) {
            return ['code' => 201, 'msg' => '缺少参数'];
        }
        $model = self::where(['id' => $params['id']])->find();
        if(!$model){
            return ['code' => 201, 'msg' => '素材不存在'];
        }
        if(!$model->allowField(true)->save($params)){
            return ['code' => 201, 'msg' => '修改失败'];
        }
        return ['code' => 200, 'msg' => '修改成功'];
    }

    public function deleteTeamSource($params = array(),$aid){
        if(!$params['id']){
            return ['code' => 201, 'msg' => '缺少参数'];
        }
        $model = self::where(['id' => $params['id']])->find();
        if(!$model){
            return ['code' => 201, 'msg' => '素材不存在'];
        }

        if(!$model->delete()){
            return ['code' => 201, 'msg' => '操作失败'];
        }
        return ['code' => 200, 'msg' => '删除成功'];

    }

    public function statusTeamSource($params = array(),$aid){
        if(empty($params['id']) || $params['status'] == NULL){
            return ['code' => 201, 'msg' => '缺少参数'];
        }
        $model = self::where(['id' => $params['id']])->find();
        if(!$model){
            return ['code' => 201, 'msg' => '素材不存在'];
        }
        $model->status = $params['status'];
        if(!$model->save()){
            return ['code' => 201, 'msg' => '下架失败'];
        }
        return ['code' => 200, 'msg' => '下架成功'];
    }




    public function getIndexTeamSource($params = array()){
        if(!$params['aid']){
            return ['code' => 200, 'msg' => 'success','data' => []];
        }
        $map = [];
        $map['yzn_source.status'] = 1;
        $map['yzn_source.is_lock'] = 0;
        $map['yzn_source.aid'] = $params['aid'];
        if ($params['cid']) {
            $map['yzn_source.cid'] = $params['cid'];
        }
        $list = SourceModel::where($map)
            ->withJoin(['cate'])
            ->order("sort desc")
            ->paginate($params['limit'], false, ['page' => $params['page']]);
        foreach ($list as $row) {
            $row->getRelation('cate')->visible(['cate_name']);
            $row->visible(['id', 'title', 'annex', 'kind']);
        }
        return ['code' => 200, 'msg' => 'success','data' => $list];
    }



}
